package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.bean.Setmeal;
import com.atguigu.constant.RedisConstant;
import com.atguigu.dao.SetmealMapper;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.service.SetmealService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private JedisPool jedisPool;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Integer[] ids, Setmeal setmeal) {
        //先添加套餐信息
        setmealMapper.addSetMeal(setmeal);
        //获取套餐id
        Integer setmealId = setmeal.getId();
        //向中间表中添加套餐信息和跟团游信息
        setmealMapper.addSetMealAndTravelGroup(setmealId,ids);
        //向redis中添加图片名称
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public List<Integer> getTravelGroupIds(Integer id) {
        return setmealMapper.getTravelGroupIds(id);
    }

    @Override
    public void edit(Integer[] ids, Setmeal setmeal) {
        setmealMapper.edit(setmeal);
        setmealMapper.deleteTravelGroupIds(setmeal.getId());
        setmealMapper.addSetMealAndTravelGroup(setmeal.getId(),ids);

    }

    @Override
    public List<Setmeal> getSetMeal() {
        return setmealMapper.getSetMeal();
    }

    @Override
    public Setmeal selectById(Integer id) {
        return setmealMapper.selectById(id);
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return setmealMapper.findSetmealCount();
    }


}
